package basetest;

import org.junit.Test;

import java.sql.*;
import java.util.Properties;

/**
 * 插入数据到数据库中
 */
public class InsertTest {
    @Test
    public void test(){
        Connection connection = null;
        PreparedStatement stmt = null;

        try {
            Properties config = new Properties();
            config.load(ClassLoader.getSystemClassLoader().getResourceAsStream("config.properties"));
            String driver = config.getProperty("Driver");
            String url = config.getProperty("url");
//          连接
            Class.forName(driver);
            connection = DriverManager.getConnection(url, config);
//            准备要发送的SQL语句
            String sql = "INSERT INTO " +
                    "new_table2(employee_id, first_name, last_name, email, phone_number, salary)" +
                    "VALUES (?, ?, ?, ?, ?, ?);";
            stmt = connection.prepareStatement(sql);
//          添加参数
            stmt.setInt(1, 1001);
            stmt.setString(2, "John");
            stmt.setString(3, "Cena");
            stmt.setString(4, "4134134314@163.com");
            stmt.setString(5, "6536735");
            stmt.setDouble(6, 12000.0);
            // 执行
            stmt.execute();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (connection != null) {
                try {
                    connection.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
            if (stmt != null) {
                try {
                    stmt.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
        }
    }
}
